package com.ioc.demo;

import com.ioc.demo.component.UserComponent;
import com.ioc.demo.config.UserConfig;
import com.ioc.demo.config.UserInfo;
import com.ioc.demo.controller.UserController;
import com.ioc.demo.repo.UserRepository;
import com.ioc.demo.service.UserService;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;

import java.applet.AppletContext;

@SpringBootApplication
public class DemoApplication {

	public static void main(String[] args) {
		//Spring上下文
		//返回Spring运行环境
		ApplicationContext context = SpringApplication.run(DemoApplication.class, args);
		UserController bean = context.getBean(UserController.class);
		bean.doController();


		UserService userService = context.getBean(UserService.class);
		userService.doService();

		//根据名称来获取
		UserService userService2 = (UserService)context.getBean("userService");
		userService2.doService();

		//根据名称＋类型
		UserService userService3 = context.getBean("userService",UserService.class);
		userService3.doService();

		UserRepository userRepository = context.getBean(UserRepository.class);
		userRepository.doRepository();


		UserComponent userComponent = context.getBean(UserComponent.class);
		userComponent.doComponent();

		UserConfig userConfig = context.getBean(UserConfig.class);
		userConfig.doConfig();

		//使用@Bean
		UserInfo userInfo = (UserInfo) context.getBean("userInfo");
		//UserInfo userInfo = context.getBean(UserInfo.class);
		UserInfo userInfo2 = context.getBean("userInfo2",UserInfo.class);
		System.out.println(userInfo);
		System.out.println(userInfo2);
	}

}
